import gs from '@/api/goods'
const state = {
  slider: [],
  keys: [],
  goodsInfo: {}
}

const mutations = {
  setGoodsInfo(state, {
    slider,
    keys,
    goodsInfo
  }) {
    state.slider = slider
    state.keys = keys
    state.goodsInfo = goodsInfo
  }
}
const getters = {
  goods: state => {
    //调试数据
    return state.keys.map(key => state.goodsInfo[key])
      .reduce((prev, next) =>
        prev.concat(next), [])
  }
}
//异步都用actions
const actions = {
  getGoods({
    state,
    commit
  }) {
    if (!state.keys.length) {
      gs.getGoodsInfo().then(goodsInfo => {
        commit('setGoodsInfo', goodsInfo)
      })
    }
  }
}

export default {
  state,
  mutations,
  getters,
  actions
}
